package com.jx.jingxuan.widget.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jx.jingxuan.widget.adapter.bean.FooterBean;
import com.jx.jingxuan.widget.adapter.bean.HeaderBean;
import com.jx.jingxuan.widget.adapter.bean.NoDataBean;

import java.util.ArrayList;
import java.util.List;

/**
 * <li>Package: {@link WbaseRecycleAdapter}
 * <li>Author: cyh
 * <li>Date: 2017/4/20 20:07
 * <li>Discription: 继承该adapter能够方便的添加header，footer和没有数据时的view
 */

public abstract class WbaseRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected List data = new ArrayList();//数据源
    private int pageSize;//一页几条
    private boolean needHeader;//是否需要header
    protected Context mContext;
    protected LayoutInflater inflater;

    private static final int FOOTER_TYPE = 0x77;//footer状态
    private static final int HEADER_TYPE = 0X88;//header状态
    private static final int NO_DATA_TYPE = 0x99;//没有数据状态
    private static final int NORMAL_TYPE = 0x11;//一般状态

    //特殊状态的实体类，现在只有布局id属性，后续可扩展
    private HeaderBean headerBean;
    private FooterBean footerBean;
    private NoDataBean noDataBean;

    public WbaseRecycleAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);

        if (getHeaderLayoutId() > 0) {
            headerBean = new HeaderBean();
            headerBean.setLayoutId(getHeaderLayoutId());
        }

        if (getFooterLayoutId() > 0) {
            footerBean = new FooterBean();
            footerBean.setLayoutId(getFooterLayoutId());
        }

        if (getNoDataLayoutId() > 0) {
            noDataBean = new NoDataBean();
            noDataBean.setLayoutId(getNoDataLayoutId());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == FOOTER_TYPE && footerBean != null) {
            return new FooterViewHolder(inflater.inflate(footerBean.getLayoutId(), parent, false));
        } else if (viewType == NO_DATA_TYPE && noDataBean != null) {
            return new NoDataViewHolder(inflater.inflate(noDataBean.getLayoutId(), parent, false));
        } else if (viewType == HEADER_TYPE && headerBean != null) {
            return new HeaderViewHolder(inflater.inflate(headerBean.getLayoutId(), parent, false));
        } else if (viewType == NORMAL_TYPE) {
            return wOnCreateViewHolder(parent, viewType);
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        if (holder instanceof HeaderViewHolder) {
            wOnBindHeaderViewHolder((HeaderViewHolder) holder, position);
        } else if (holder instanceof FooterViewHolder) {
            wOnBindFooterViewHolder((FooterViewHolder) holder, position);
        } else if (holder instanceof NoDataViewHolder) {
            wOnBindNoDataViewHolder((NoDataViewHolder) holder, position);
        } else {
            wOnBindViewHolder(holder, position);
        }
    }

    public abstract void wOnBindViewHolder(RecyclerView.ViewHolder holder, int position);

    public abstract RecyclerView.ViewHolder wOnCreateViewHolder(ViewGroup parent, int viewType);

    public abstract int getHeaderLayoutId();

    public abstract int getFooterLayoutId();

    public abstract int getNoDataLayoutId();

    public void wOnBindHeaderViewHolder(HeaderViewHolder headerViewHolder, int position) {

    }

    public void wOnBindFooterViewHolder(FooterViewHolder footerViewHolder, int position) {

    }

    public void wOnBindNoDataViewHolder(NoDataViewHolder noDataViewHolder, int position) {

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    /**
     * 是否是没有数据的状态
     *
     * @return
     */
    private boolean isNoDataType() {
        if (getRealDataCount() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 是否需要增加footer
     *
     * @param data
     * @return
     */
    private boolean isFooterType(List data) {

        if (getRealDataCount() % pageSize != 0 || (data.size() == 0 && this.data.size() > 0)) {
            return true;
        }
        return false;
    }

    /**
     * 是否需要增加header
     *
     * @return
     */
    private boolean isHeaderType() {
        return needHeader;
    }

    /**
     * 是否header和footer都需要
     *
     * @param data
     * @return
     */
    private boolean isHeaderAndFooterType(List data) {
        if (isFooterType(data) && needHeader) {
            return true;
        }
        return false;
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(List data) {
        if (data == null) {
            data = new ArrayList();
        }
        removeViews();
        this.data.addAll(data);
        if (isHeaderAndFooterType(data)) {
            addHeaderData();
            addFooterData();
        } else if (isNoDataType()) {
            addNoData();
        } else if (isFooterType(data)) {
            addFooterData();
        } else if (isHeaderType()) {
            addHeaderData();
        }
        notifyDataSetChanged();
    }

    /**
     * 设置数据之前需要移除footer，nodata view
     */
    private void removeViews() {
        if (data == null) {
            return;
        }
        if (data.contains(footerBean)) {
            data.remove(footerBean);
        }
        if (data.contains(noDataBean)) {
            data.remove(noDataBean);
        }
        if (!needHeader) {
            data.remove(headerBean);
        }
    }

    /**
     * 增加header数据
     */
    private void addHeaderData() {
        if (data != null && headerBean != null) {
            if (data.size() == 0) {
                data.add(headerBean);
            } else {
                if (!(data.get(0) instanceof HeaderBean)) {
                    data.add(0, headerBean);
                }
            }
        }
    }

    /**
     * 增加footer数据
     */
    private void addFooterData() {
        if (data != null && footerBean != null) {
            if (data.size() == 0) {
                data.add(footerBean);
            } else {
                if (!(data.get(data.size() - 1) instanceof FooterBean)) {
                    data.add(data.size(), footerBean);
                }
            }
        }
    }

    /**
     * 增肌没有数据状态
     */
    private void addNoData() {
        if (data != null && noDataBean != null) {
            data.add(noDataBean);
        }
    }

    /**
     * 清除数据
     */
    public void clear() {
        this.data.clear();
        notifyDataSetChanged();
    }

    /**
     * 获得真实的数据，除HeaderBean,FooterBean,NoDataBean
     */
    private int getRealDataCount() {
        int count = 0;
        if (data != null) {
            count = data.size();
            if (data.contains(headerBean)) {
                count--;
            }
            if (data.contains(footerBean)) {
                count--;
            }
            if (data.contains(noDataBean)) {
                count--;
            }
        }
        return count;
    }

    /**
     * 设置一页的数据量，用于判断是否要增加footer
     *
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setNeedHeader(boolean needHeader) {
        this.needHeader = needHeader;
    }

    @Override
    public int getItemViewType(int position) {
        Object o = data.get(position);
        if (o instanceof HeaderBean) {
            return HEADER_TYPE;
        } else if (o instanceof FooterBean) {
            return FOOTER_TYPE;
        } else if (o instanceof NoDataBean) {
            return NO_DATA_TYPE;
        } else {
            return NORMAL_TYPE;
        }
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        private View itemView;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public View getItemView() {
            return itemView;
        }
    }

    public static class FooterViewHolder extends RecyclerView.ViewHolder {
        private View itemView;

        public FooterViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public View getItemView() {
            return itemView;
        }
    }

    public static class NoDataViewHolder extends RecyclerView.ViewHolder {
        private View itemView;

        public NoDataViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public View getItemView() {
            return itemView;
        }
    }
}
